﻿'Author: Jihad Abouhatab
'3/21/12
'This page is for a adding a new course and displaying courses in a gridview and input validation



Imports DBUtilitiesLibrary
Imports DARSClassLibrary
Public Class addCourse
    Inherits System.Web.UI.Page

    ' Private currentAdvisor As Advisor = CType(Session("CurrentUser"), Advisor)
    ' Private currentAdvisor As Advisor = New Advisor()
    Dim SQL As String
    Dim DBobj As New DARSDBConnection
    Dim DS As DataSet
    Dim ws As DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if user is logged in
        'txtCourseID.Enabled = False
        'txtCourseName.Enabled = False
        'txtDescription.Enabled = False
        'txtEarnedCredits.Enabled = False


        If Session("CurrentUser") Is Nothing Then
            Response.Redirect("NotLoggedIn.aspx")
            Exit Sub
        End If
        If Not IsPostBack Then
            ws = New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice
            Session("Pxy") = ws

            
            DropDownList1.Visible = False
            Button1.Visible = False
            Label1.Visible = False
            ' GridView1.Visible = False

        Else
            ws = CType(Session("Pxy"), DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice)

        End If
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click

        If txtCourseID.Text = "" Then
            lblValidate.Text = "CourseID cannot be empty"
            Exit Sub
        ElseIf txtCourseName.Text = "" Then
            lblValidate.Text = "Course Name cannot be empty"
            Exit Sub
        ElseIf IsNumeric(txtCourseName.Text) Then
            lblValidate.Text = "Course Name must only be letters"
            Exit Sub
        ElseIf txtEarnedCredits.Text = "" Then
            lblValidate.Text = "Earned Credits cannot be empty"
            Exit Sub
        ElseIf Not IsNumeric(txtEarnedCredits.Text) Then
            lblValidate.Text = " Earned Credits must be numaric"
            Exit Sub
        Else
            ws = New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice

            ' Dim sql As String = "Insert Into Course Values('" & txtCourseID.Text & "','" & txtCourseName.Text.Trim & "','" & txtDescription.Text.Trim & "','" & ddlDepartmentID.SelectedValue & "','" & txtEarnedCredits.Text.Trim & "' )"
            Dim CourseID As String = txtCourseID.Text.Trim
            Dim CourseName As String = txtCourseName.Text.Trim
            Dim Description As String = txtDescription.Text
            Dim DepartmentID As String = ddlDepartmentID.SelectedValue 'currentAdvisor.DepartmentID
            Dim EarnedCredits As String = txtEarnedCredits.Text

            'I DON'T THINK YOU NEED THE FOLLOWING LINES. ALL YOU NEED IN THE PAGE IS DISPLAY A MESSAGE THAT SAYS " COURSE SUCCESSFULLY ADDED" OR SOMETHING
            'OR AN ERROR MESSAGE IF THE COURSEID ALREADY EXISTS
            'I don't think you need this Dropdownlist anymore use the department of the advisor who is logged in to add this course "see code behind

            ws.addCourse(CourseID, CourseName, Description, DepartmentID, EarnedCredits)
            DS = ws.getCourseInfo(txtCourseID.Text)
            GridView1.DataSource = DS
            GridView1.DataBind()
            lblNotifucation.Text = "Course " & CourseID & ", " & CourseName & " successfully added"
            lblValidate.Text = ""

            txtCourseID.Visible = True
            txtCourseName.Visible = True
            txtDescription.Visible = True
            txtEarnedCredits.Visible = True
            Label1.Visible = True

            txtCourseID.Enabled = False
            txtCourseName.Enabled = False
            txtDescription.Enabled = False
            txtEarnedCredits.Enabled = False
            ddlDepartmentID.Enabled = False
            DropDownList1.Visible = True
            Button1.Visible = True
            Label1.Visible = True
            gvCourses.Visible = False

        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        ' ws = New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice

        ws.assignRequirementToCourse(CInt(DropDownList1.Text), txtCourseID.Text)
        lblNotification2.Text = "Requirement ID " & DropDownList1.SelectedValue & " added to Course " & txtCourseName.Text & ""
        Label1.Visible = False
        DropDownList1.Visible = False
        Button1.Visible = False
    End Sub

   
    
    Protected Sub ddlDepartmentID_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlDepartmentID.SelectedIndexChanged
        'Session("Pxy") = ws
        ' Dim s As String = "SELECT * FROM Course WHERE CourseID='" & ddlDepartmentID.Text & "'"
        Dim ds As DataSet = ws.getCourses(ddlDepartmentID.Text)
        gvCourses.DataSource = ds
        gvCourses.DataBind()
        txtCourseID.Visible = True
        txtCourseName.Visible = True
        txtDescription.Visible = True
        txtEarnedCredits.Visible = True
    End Sub

    Protected Sub logout(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.ServerClick
        Session.Abandon()
        Response.Redirect("NotLoggedIn.aspx")
    End Sub

    Protected Sub home(ByVal sender As Object, ByVal e As EventArgs) Handles btnhome.ServerClick
        Response.Redirect("AdvisorLandingPage.aspx")
    End Sub
End Class